<!DOCTYPE html>

<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <title>Looping / Learn Vimscript the Hard Way</title>
        <meta name="description" content="">
        <meta name="author" content="Steve Losh">
        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->

        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

        <link href="/static/styles/skeleton/base.css" rel="stylesheet" type="text/css" />
        <link href="/static/styles/skeleton/skeleton.css" rel="stylesheet" type="text/css" />
        <link href="/static/styles/skeleton/layout.css" rel="stylesheet" type="text/css" />

        <link href="/static/styles/tango.css" rel="stylesheet" type="text/css" />
        <link href="/static/styles/style.less" rel="stylesheet/less" type="text/css" />

        <script type="text/javascript" src="/static/scripts/less.js"></script>
    </head>

    <body class="">
        <div class="container">
            <header class="sixteen columns">
                <h1><a href="/">Learn Vimscript the Hard Way</a></h1>
            </header>

            
    <section class="nav three columns">
        
<ul>
<li><a href="#looping">Looping</a><ul>
<li><a href="#for-loops">For Loops</a></li>
<li><a href="#while-loops">While Loops</a></li>
<li><a href="#exercises">Exercises</a></li>
</ul>
</li>
</ul>


        <div class="prevnext">
            
                <a class="prev" href="35.html">&laquo; Prev</a>
            
            
                <a class="next" href="37.html">Next &raquo;</a>
            
        </div>
    </section>

    <section class="content twelve columns offset-by-one">
        <div> 
<h1 id="looping">Looping</h1>
<p>You might be surprised to realize that we've gone through thirty five chapters
of a programming language book without even mentioning loops!  Vimscript offers
so many other options for performing actions on text (<code>normal!</code>, etc) that loops
aren't as necessary as they are in most other languages.</p>
<p>Even so, you'll definitely need them some day, so now we'll take a look at the
two main kinds of loops Vim supports.</p>
<h2 id="for-loops">For Loops</h2>
<p>The first kind of loop is the <code>for</code> loop.  This may seem odd if you're used to
Java, C or Javascript <code>for</code> loops, but turns out to be quite elegant.  Run the
following commands:</p>
<pre class="codehilite"><code class="language-vim">:let c = 0

:for i in [1, 2, 3, 4]
:  let c += i
:endfor

:echom c</code></pre>


<p>Vim displays <code>10</code>, which is the result of adding together each element in the
list.  Vimscript <code>for</code> loops iterate over lists (or dictionaries, which we'll
cover later).</p>
<p>There's no equivalent to the C-style <code>for (int i = 0; i &lt; foo; i++)</code> loop form in
Vimscript.  This might seem bad at first, but in practice you'll never miss it.</p>
<h2 id="while-loops">While Loops</h2>
<p>Vim also supports the classic <code>while</code> loop.  Run the following commands:</p>
<pre class="codehilite"><code class="language-vim">:let c = 1
:let total = 0

:while c &lt;= 4
:  let total += c
:  let c += 1
:endwhile

:echom total</code></pre>


<p>Once again Vim displays <code>10</code>.  This loop should be familiar to just about anyone
who's programmed before, so we won't spend any time on it.  You won't use it
very often.  Keep it in the back of your mind for the rare occasions that you
want it.</p>
<h2 id="exercises">Exercises</h2>
<p>Read <code>:help for</code>.</p>
<p>Read <code>:help while</code>.</p></div>

        <div class="prevnext">
            
                <a class="prev" href="35.html">&laquo; Previous</a>
            
            
                <a class="next" href="37.html">Next &raquo;</a>
            
        </div>
    </section>


            <footer class="sixteen columns">
                Made by <a href="http://stevelosh.com">Steve Losh</a>.

                <a href="/license.html">License</a>.

                Built with
                <a href="http://bitbucket.org/sjl/bookmarkdown/">Bookmarkdown</a>.
            </footer>
        </div>

        
            <script type="text/javascript">
                var _gaq = _gaq || [];
                _gaq.push(['_setAccount', 'UA-15328874-8']);
                _gaq.push(['_trackPageview']);

                (function() {
                 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                 })();
            </script>
        

        
            <script type="text/javascript">
                var _gauges = _gauges || [];
                (function() {
                 var t   = document.createElement('script');
                 t.type  = 'text/javascript';
                 t.async = true;
                 t.id    = 'gauges-tracker';
                 t.setAttribute('data-site-id', '4e8f83b7f5a1f546e200000d');
                 t.src = '//secure.gaug.es/track.js';
                 var s = document.getElementsByTagName('script')[0];
                 s.parentNode.insertBefore(t, s);
                 })();
             </script>
        
    </body>
</html>
